/**
 * 研讨
 */
//记录本地已有的研讨id
var currDiscussids;
//记录所有研讨数据，以便存储到本地
var discussList;
var localStorageManagers = require("script/localStorageManagers.js");
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack:true
});
//plus初始化
mui.plusReady(function(){
	plusReady();
});
//
function plusReady(){
	//滚动条
	mui('.mui-scroll-wrapper').scroll({
					indicators: false //是否显示滚动条
				});
	//加载研讨列表
	loadDiscussDiv();
	//打开研讨页面
	mui('.discusss-div').on('tap','.discuss-div',openDiscuss);
}
//加载研讨列表
function loadDiscussDiv(){
	if(hasNet()){
		document.getElementById('nettip').style.display="none";
		ajaxLoadDiscuss();
	}else{
		document.getElementById('nettip').style.display="block";
	}
}
//是否有网
function hasNet() {
	var isNet = mobileFrame.IsNetWorkCanUse();
	return isNet;
}
//ajax加载研讨列表
function ajaxLoadDiscuss(){
	var userid=localStorageManagers.getUseridSession();
	var url=serverAjax+"mobile/studycenter/getDiscussList.do";
	var w=plus.nativeUI.showWaiting();
	mui.ajax(url,{
		type:'post',
		dataType:'json',
		data:{userid:userid},
		timeout:10000,
		success:function(data){
			if(data.length==0){
				w.close();
				return;
			}
			for (var i = 0; i < data.length; i++) {
				var discuss = data[i];
				//discussList.push(discuss);
				buildDiscussDiv(discuss);
			}
			w.close();
		},
		error:function(xhr, type, errorThrown) {
			ajaxErrorTip();
			w.close();
			console.log(xhr);
		}
		
	});
};
//
function buildDiscussDiv(discuss){
	var discusssdiv=document.getElementById('discusss-div');
	//研讨类型
	var distype='<span class="d-type v">视频研讨</span>';
	if(discuss.type!=1)distype='<span class="d-type t">文本研讨</span>';
	//评论
	var commontotle="暂无";
	if(discuss.pinglun>0)commontotle=discuss.pinglun;
	//班级
	var classnamestr='';
	var classnames=discuss.classNmae.split(',');
	for(var i=0;i<classnames.length;i++){
		//if(i%3==0)classnamestr+='<div class="row">';
		var classname=classnames[i];
		if(classname.length>4)classname=classname.substring(0,3)+'...';
		classnamestr+='<span class="classname"><span class="circle">●</span>'+classname+'</span>';
		//if(i%3==0)classnamestr+='</div>';
	}
	var disstr='<div class="discuss-div" discussid="'+discuss.id+'" discussname="'+discuss.title+'">'
					+'<div class="top">'
						+distype
						+'<span class="d-name">'+discuss.title+'</span>'
						
					+'</div>'
					+'<div class="center">'+classnamestr+'</div>'
					+'<div class="foot">'
						+'<span>评论数:</span>'
						+'<span class="comment-totle">'+commontotle+'</span>'
						+'<span class="d-time">'+discuss.createTime+'</span>'
					+'</div>'
				+'</div>';
	discusssdiv.innerHTML=discusssdiv.innerHTML+disstr;
	
}
//打开研讨页面
function openDiscuss(){
	var discussid=this.getAttribute("discussid");
	var discussname=this.getAttribute("discussname");
	mui.openWindow({
		url:'discussstudy.html',
		id:'selfcenter/classdiscuss/discussstudy',
		extras:{'discussid':discussid,'discussname':discussname}
	});
}
